﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Frame.Lib.Data
{
    public class ConnectionStringManager
    {

        private static Dictionary<string, string> connectionStrings = new Dictionary<string, string>();

        private const string KEY = "jzycyl";


        public static string Get(string encryptString)
        {
            lock (connectionStrings)
            {
                if (!connectionStrings.ContainsKey(encryptString))
                {
                    string str = null;
                    try
                    {
                        str = Decrypt(encryptString);

                    }
                    catch
                    {
                        str = encryptString;
                    }

                    connectionStrings.Add(encryptString, str);
                }
                return connectionStrings[encryptString];
            }
        }

        #region 获得DES加密所需定长64位密钥
        /// <summary>
        /// 获得DES加密所需定长64位密钥
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static byte[] GetDESKey(string str)
        {
            byte[] bKey = new byte[8];
            byte[] bTemp = Encoding.UTF8.GetBytes(str);

            for (int i = 0; i < 8; i++)
            {
                if (i <= bTemp.Length - 1)
                    bKey[i] = bTemp[i];
                else
                    bKey[i] = (byte)0;
            }
            return bKey;

        }
        #endregion

        private static string Encrypt(string strSrc)
        {
            if (strSrc == null)
            {
                return null;
            }
            DES des = new DESCryptoServiceProvider();// DESCryptoServiceProvider();
            des.Key = GetDESKey(KEY);
            des.IV = des.Key;
            MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            byte[] bTxt = Encoding.UTF8.GetBytes(strSrc);

            cs.Write(bTxt, 0, bTxt.Length);
            cs.Flush();
            cs.FlushFinalBlock();//不能少！！！y5mIPYebHkc=  y5mIPYebHkc=

            byte[] bEncrypt = ms.ToArray();
            string strResult = Convert.ToBase64String(bEncrypt);
            cs.Close();
            ms.Close();
            return strResult;
        }

        private static string Decrypt(string strSrc)
        {
            if (strSrc == null)
            {
                return null;
            }
            DES des = new DESCryptoServiceProvider();
            des.Key = GetDESKey(KEY);
            des.IV = des.Key;

            MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

            byte[] bTemp = Convert.FromBase64String(strSrc);
            string strResult = null;
            try
            {
                cs.Write(bTemp, 0, bTemp.Length);
                cs.FlushFinalBlock();
                byte[] bTxt = ms.ToArray();
                strResult = Encoding.UTF8.GetString(bTxt);
            }
            catch
            {
                throw new Exception("密钥不对");
            }
            finally
            {
                cs.Close();
                ms.Close();
            }
            return strResult;
        }
    }
}
